# coding: utf-8
# Author: legend
# @Time： 2020/8/27 下午10:05
import logging

from utils.messages.handler import BaseHandler
from websocket.messages import LatestServiceRunningDataResponse, MultiServiceRunningDataResponse

error = logging.getLogger('app.error.log')


class LatestServiceRunningDataRequestHandler(BaseHandler):

    async def do(self, *args, **kwargs):
        service_id = kwargs.get('service_id')
        data = self.get_monitor_data(1, service_id, 'service.status', 1)
        return {
            'data': data,
            'result_code': LatestServiceRunningDataResponse.ResultCode.NORMAL
        }


class MultiServiceRunningDataRequestHandler(BaseHandler):

    async def do(self, *args, **kwargs):
        service_id = kwargs.get('service_id')
        size = kwargs.get('size')
        data = self.get_monitor_data(1, service_id, 'service.status', size)
        return {
            'data': list(reversed(data)),
            'result_code': MultiServiceRunningDataResponse.ResultCode.NORMAL
        }

# class ServiceOnDeviceRequestHandler(BaseHandler):
#
#     async def do(self, *args, **kwargs):
#         device_id = kwargs.get('device_id')
#         try:
#             result = []
#             services = await Service.filter(device=device_id)
#             for service in services:
#                 service_run_data = ServiceRunningData.objects.filter(service_id=service.id) \
#                     .order_by('-collect_timestamp')
#                 data = ServiceRunningDataSerializer(
#                     instance=service_run_data.first(),
#                     fields=('service_id', 'cpu_usage', 'memory')
#                 ).data
#                 result.append(data)
#             return {'data': result, 'result_code': ServiceOnDeviceResponse.ResultCode.NORMAL}
#         except Exception as e:
#             error.exception(e)
#             return {'data': None, 'result_code': ServiceOnDeviceResponse.ResultCode.NO_DATA}
